import asyncio
import time
import threading
# 定义一个耗时的任务
async def fake_io_operation(name, delay):
    print(f"开始执行{name}任务")
    await asyncio.sleep(delay)  # 模拟IO阻塞操作，如网络请求或文件读写
    result = f"{name}任务完成"
    print(result)
    return result

# 异步调用上述任务
async def main():
    task1 = asyncio.create_task(fake_io_operation("任务一", 3))
    task2 = asyncio.create_task(fake_io_operation("任务二", 1))

    print("启动所有任务...")
    
    # 使用await等待所有任务完成
    result1 = await task1
    result2 = await task2
    
    print(f"任务一结果: {result1}")
    print(f"任务二结果: {result2}")

def taskX(num,name,callback):
    print(f"Task {name} started with num: {num}")
    time.sleep(2)  # 模拟耗时操作
    result = num * num
    print(f"Task {name} finished. Result: {result}")
    callback(result)

def taskX2(str):
    time.sleep(2)  # 模拟耗时操作
    print(f"Task {str} with num: {str}")




def startX():
    print("gogo")
    #asyncio.run(main())   
    callback_function = lambda result: print(f"Callback function called. Result: {result}")
    # threading.Thread(target=taskX, args=(1,"任务A",callback_function)).start()
    # threading.Thread(target=taskX, args=(2,"任务B",callback_function)).start()
    threading.Thread(target=taskX2, args=("任务A",)).start()
    threading.Thread(target=taskX2, args=("任务B",)).start()
    print("endend")

# 开个线程执行
startX()

